﻿Imports System.Data.SqlClient
Public Class FrmBaoCaoSoBoDiaChinhKhaiThac
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHuyen.DisplayMember = "T_huyen"
        ComboBoxHuyen.ValueMember = "maH"
        ComboBoxHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDMTinhTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTinhTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHienTrang.DisplayMember = "TenLuuKho"
        ComboBoxHienTrang.ValueMember = "MaTinhTrangId"
        ComboBoxHienTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub

    Private Sub ComboBoxHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxHuyen.SelectedIndexChanged
        ' load ten huyen
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & ComboBoxHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxXa.DisplayMember = "TenXa"
        ComboBoxXa.ValueMember = "maxa"
        ComboBoxXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub DataGridViewBanDo_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBanDo.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBanDo.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBanDo.RowHeadersWidth < Size.Width + 20 Then DataGridViewBanDo.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub

    Private Sub FrmBaoCaoBanDoDiaChinhDaChoMuon_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NapDonViHuyen()
        NapDMTinhTrang()
        DateTimePickerTuNgay.Text = DateAdd(DateInterval.Year, -1, Today())
        DateTimePickerDenNgay.Text = Today()
        DataGridViewBanDo.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBanDo.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub
    Private Sub NapBanDoDiaChinh()
        Dim TuNgay As String
        TuNgay = String.Format("{0:MM/dd/yyyy}", DateTimePickerTuNgay.Value)
        Dim DenNgay As String
        DenNgay = String.Format("{0:MM/dd/yyyy}", DateTimePickerDenNgay.Value)
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  m.NgayMuon, m.NgayTra, m.quyenso,  m.NamThanhLap, x.TenXa, h.T_huyen, NguoiMuon," &
                                " DonViCongTac, DiaChi, MucDichMuon, tl.TenLoaiTaiLieu, sb.tensobo" &
                                    " From  SoboDiachinhMuon m, tenhuyen h, tenxa x, DMTaiLieu tl, DMtinhTrang tt,  DMSoBo sb " &
                                    " WHERE " &
                                    " m.MahuyenID= '" & ComboBoxHuyen.SelectedValue.ToString & "' " &
                                    " and m.MahuyenID = h.mah " &
                                    " and m.Maxaid = '" & ComboBoxXa.SelectedValue.ToString & "'" &
                                    " and m.maxaid = x.maxa" &
                                    " and m.matinhtrangid = '" & ComboBoxHienTrang.SelectedValue.ToString & "' " &
                                    " and m.matinhtrangID = tt.matinhtrangid " &
                                    " and m.maLoaiTaiLieuID = tl.MaLoaiTaiLieuID " &
                                    " and m.MaLoaiSoBoID = sb.MaSobo " &
                                    " and datediff (day, ngaymuon, '" & TuNgay & "') <= 0  " &
                                     " and datediff (day, ngaymuon, '" & DenNgay & "') >= 0  order by ngaymuon ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewBanDo.DataSource = tbl
        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("không tìm thấy thông tin !")
        Else
        End If
        ob_cnn.Close()

    End Sub
    Private Sub XuatExcel()
        ' xuat truc tiep ra excel
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "D:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "Ban do cap huyen "
                Dim i As Integer = 1
                For col = 0 To DataGridViewBanDo.Columns.Count - 1
                    .cells(1, i).value = DataGridViewBanDo.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridViewBanDo.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridViewBanDo.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridViewBanDo.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Dữ liệu xuất ra Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub
    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub

    Private Sub ButtonTimKiem_Click(sender As Object, e As EventArgs) Handles ButtonTimKiem.Click
        NapBanDoDiaChinh()

    End Sub

    Private Sub ButtonXuatExcel_Click(sender As Object, e As EventArgs) Handles ButtonXuatExcel.Click
        XuatExcel()
    End Sub

    Private Sub DateTimePickerDenNgay_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePickerDenNgay.ValueChanged

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub
End Class